<?php
//require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_good_update_action_service extends background_good_add_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        //var_dump(__METHOD__); exit();
                
        $result = array('status' => 0, 'message' => '', 'data' => array());
		    
	    $check_save_data_result = $this->check_save_data();	    
	    if($check_save_data_result['status'] < 0){
	        $result['status'] = $check_save_data_result['status'];
	        $result['message'] = $check_save_data_result['message'];
	    }else{
	        $good_info = $check_save_data_result['data']['good_info'];
	        $good_id = $good_info['id'];
    	    
    	    
    	    $write_db = $this->_get_write_db();
    	    $pdo = $write_db->get_connection();
    	    $pdo->beginTransaction();
    	    
    	    try{	        	        
        	    $model_background_good = wwy_resource_container::get_model_background_good();
        	    
        	    $update_time = time();
        	    $good_info['update_time'] = $update_time;
        	    $row_count = $model_background_good->update($good_info);
    	        if($row_count === false){
                    $result['status']  = -400;
                    $result['message'] = '修改失败';
    	        }else{    	    	        
        	        //删除相关【属性、规格、规格属性、价格范围】
        	        $remove_attr_spec_data_result = $model_background_good->remove_attr_spec_data($good_id);
            	    
            	    $good_attr_list = $good_info['attr_list'];
            	    $model_background_good->add_good_attr_list($good_info, $good_attr_list);
            	    
            	    $good_sattr_list = $good_info['sattr_list'];
            	    $good_sattr_list = $model_background_good->add_good_sattr_list($good_info, $good_sattr_list);
            	    
            	    $good_number_grade_list = $good_info['number_grade_list'];
            	    $good_number_grade_list = $model_background_good->add_good_number_grade_list($good_info, $good_number_grade_list);
            	    
            	    $good_spec_list = $good_info['spec_list'];
            	    $model_background_good->add_good_spec_list($good_info, $good_spec_list, $good_sattr_list, $good_number_grade_list);                       	      
            	       	    
            	    $result['status']  = 1;
            	    $result['message'] = '更新商品成功';
            	    self::add_view_data('good_id', $good_id);
            	    
            	    wwy_resource_container::get_cache_handler()->flush();
        	    
        			$pdo->commit();
    	        }
    		}catch (Exception $e){
    			$pdo->rollBack();
    			
    			throw $e;
    		}	
	    }
	    
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}